home *** CD-ROM | disk | FTP | other *** search
/ mac CD 77 / Image.iso / macware / BlackWatch 1.5.5 / Extras / For programmers / TestGestalt source / TestGestalt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-18  |  5.4 KB  |  239 lines  |  [TEXT/CWIE]

  1. // TestGestalt.c
  2. //
  3. // simple test of Gestalt SAVC commands to control a screen saver
  4. //
  5.  
  6. #include <Types.h>
  7. #include <Memory.h>
  8. #include <Quickdraw.h>
  9. #include <Fonts.h>
  10. #include <Events.h>
  11. #include <Menus.h>
  12. #include <Windows.h>
  13. #include <TextEdit.h>
  14. #include <Dialogs.h>
  15. #include <OSUtils.h>
  16. #include <ToolUtils.h>
  17. #include <SegLoad.h>
  18. #include <Sound.h>
  19.  
  20. #include "TestGestalt.h"
  21.  
  22. /* prototypes */
  23. void Initialize(void);
  24. void EventLoop(void);
  25. void DoTest(short item);
  26. void DoMouseDown(EventRecord *theEvent);
  27. void DoKeyDown(EventRecord *theEvent);
  28. void DoMenu(long menuResult);
  29.  
  30. static Boolean gQuit = false;
  31.  
  32. /*------------------------------------------------------------------------*/
  33. main(void)
  34. {
  35.     Initialize();
  36.     EventLoop();
  37.     return 0;
  38. }
  39.  
  40. /*------------------------------------------------------------------------*/
  41. void Initialize(void)
  42. {
  43.     Handle theMBarHandle;
  44.     MenuHandle theAppleMenu;
  45.     DialogPtr theDialog;
  46.  
  47.     // Initialize toolbox managers.
  48.     InitGraf(&qd.thePort);
  49.     InitFonts();
  50.     InitWindows();
  51.     InitMenus();
  52.     TEInit();
  53.     InitDialogs(nil);
  54.     InitCursor();
  55.     
  56.     // Set up the menus.
  57.     theMBarHandle = GetNewMBar(MBAR_ID);
  58.     SetMenuBar(theMBarHandle);
  59.     theAppleMenu = GetMenuHandle(APPLE_ID);
  60.     AppendResMenu(theAppleMenu, 'DRVR');
  61.     DrawMenuBar();
  62.  
  63.     // Put up our dialog.
  64.     theDialog = GetNewDialog(TEST_DLOG, nil, (WindowRef) -1L);
  65. }
  66.  
  67. /*------------------------------------------------------------------------*/
  68. void EventLoop(void)
  69. {
  70.     EventRecord event;
  71.     Boolean gotEvent;
  72.     short item = 0;
  73.     DialogPtr dp;
  74.  
  75.     while (!gQuit)
  76.     {
  77.         gotEvent = WaitNextEvent(everyEvent, &event, 0L, nil);
  78.         if (gotEvent) {
  79.             if (IsDialogEvent(&event)) {
  80.                 if (DialogSelect(&event, &dp, &item))
  81.                     if (item == QUIT_BTN)
  82.                         gQuit = true; // Quit button was pressed
  83.                     else
  84.                         DoTest(item);
  85.             }
  86.             switch (event.what) {
  87.                 case mouseDown:
  88.                     DoMouseDown(&event);
  89.                     break;
  90.                 case keyDown:
  91.                 case autoKey:
  92.                     DoKeyDown(&event);
  93.                     break;
  94.             }
  95.         }
  96.     }
  97. }
  98.  
  99. /*------------------------------------------------------------------------*/
  100. void DoTest(short item)
  101. {
  102.     // call Gestalt to get the control procedure pointer
  103.     SaverControlUPP theControlProc;
  104.     OSErr theErr = Gestalt(gestaltScreenSaverControl, (long*)&theControlProc);
  105.     if (theErr != noErr)
  106.         return;
  107.  
  108.     switch (item) {
  109.         case SLEEP_BTN: {    // Sleep
  110.             // call the control procedure with the "sleep now" command
  111.             CallSaverControlProc(theControlProc, gestaltSaverSleep);
  112.             break;
  113.         }
  114.         case DSLEEP_BTN: {    // Deep Sleep
  115.             unsigned long wakeTime = TickCount() + (5*60);
  116.             
  117.             // put the screen saver into "deep sleep" mode
  118.             CallSaverControlProc(theControlProc, gestaltSaverDeepSleep);
  119.             
  120.             // wait 5 seconds, then wake it up
  121.             while (TickCount() < wakeTime) {
  122.                 EventRecord dummyEvt;
  123.                 (void) WaitNextEvent(nullEvent, &dummyEvt, 0L, nil);
  124.             }
  125.             CallSaverControlProc(theControlProc, gestaltSaverWakeUp);
  126.             break;
  127.         }
  128.         case OFF_BTN: {    // Turn Off
  129.             // call the control procedure with the "off" command
  130.             CallSaverControlProc(theControlProc, gestaltSaverOff);
  131.             break;
  132.         }
  133.         case ON_BTN: {    // Turn On
  134.             // call the control procedure with the "on" command
  135.             CallSaverControlProc(theControlProc, gestaltSaverOn);
  136.             break;
  137.         }
  138.     }
  139. }
  140.  
  141. /*------------------------------------------------------------------------*/
  142. void DoMouseDown(EventRecord *theEvent)
  143. {
  144.     register short code;
  145.     WindowPtr whichWindow;
  146.     RgnHandle boundsRgn;
  147.     long newSize;
  148.  
  149.     boundsRgn = GetGrayRgn();
  150.     code = FindWindow(theEvent->where, &whichWindow);
  151.  
  152.     switch (code) {
  153.         case inMenuBar:
  154.             DoMenu(MenuSelect(theEvent->where));
  155.             break;
  156.         case inSysWindow: 
  157.             SystemClick(theEvent, whichWindow);
  158.             break;
  159.         case inContent:
  160.             if (whichWindow != FrontWindow())
  161.                 SelectWindow(whichWindow);
  162.             break;
  163.         case inDrag:
  164.             DragWindow(whichWindow, theEvent->where, &(*boundsRgn)->rgnBBox);
  165.             break;
  166.         case inGrow:
  167.             newSize = GrowWindow(whichWindow, theEvent->where, &(*boundsRgn)->rgnBBox);
  168.             if (newSize)
  169.             {
  170.                 SetPort(whichWindow);
  171.                 EraseRect(&whichWindow->portRect);
  172.                 SizeWindow(whichWindow, LoWord(newSize), HiWord(newSize), true);
  173.                   InvalRect(&whichWindow->portRect); /* erase scroll bar marks */
  174.             }
  175.             break;
  176.         case inGoAway:
  177.             if (TrackGoAway(whichWindow, theEvent->where))
  178.                 DisposeWindow(whichWindow);
  179.             break;
  180.         case inZoomIn:
  181.         case inZoomOut:
  182.             if (TrackBox(whichWindow, theEvent->where, code))
  183.             {
  184.                 SetPort(whichWindow);
  185.                 EraseRect(&whichWindow->portRect);
  186.                 ZoomWindow(whichWindow, code, TRUE);
  187.                 InvalRect(&whichWindow->portRect); /* erase scroll bar marks */
  188.             }
  189.             break;
  190.     }
  191. }
  192.  
  193. /*------------------------------------------------------------------------*/
  194. void DoKeyDown(EventRecord *theEvent)
  195. {
  196.     register short c;
  197.     
  198.     c = (unsigned char)(theEvent->message & charCodeMask);
  199.     if (theEvent->modifiers & cmdKey)
  200.         DoMenu(MenuKey(c));
  201. }
  202.  
  203. /*------------------------------------------------------------------------*/
  204. void DoMenu(long menuResult)
  205. {
  206.     short menuID, itemNumber;
  207.     Str255 daName;
  208.     GrafPtr oldPort;
  209.  
  210.     menuID = HiWord(menuResult);
  211.     itemNumber = LoWord(menuResult);
  212.     
  213.     switch (menuID) {
  214.         case APPLE_ID:
  215.             if (itemNumber != ABOUT_ITEM)
  216.             {
  217.                 GetMenuItemText((MenuRef)GetMenuHandle(APPLE_ID), itemNumber, daName);
  218.                 GetPort(&oldPort);
  219.                 OpenDeskAcc(daName);
  220.                 SetPort(oldPort);
  221.             }
  222.             break;
  223.         case FILE_ID:
  224.             if (itemNumber == QUIT_ITEM)
  225.                 gQuit = true;
  226.             break;
  227.         case EDIT_ID:
  228.             if (itemNumber <= CLEAR_ITEM)
  229.                 SystemEdit(itemNumber-1);
  230.             break;
  231.         default:
  232.             break;
  233.     }
  234.     HiliteMenu(0);
  235. }
  236.  
  237.  
  238.  
  239.